package com.start.pay.core;

import cn.hutool.core.date.LocalDateTimeUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.time.LocalDateTime;

@Data
@Configuration
@ConfigurationProperties(prefix = "start.pay")
public class StartGlobalPayConfig {

    @Schema(name = "过期时间偏移量", description = "单位秒")
    private Integer expireTime;
    @Schema(name = "拉取支付时间偏移量", description = "单位秒")
    private Integer payTime;
    @Schema(name = "是否启用定时任务")
    private boolean quartzEnable;
    @Schema(name = "定时任务名称")
    private String quartzName;
    @Schema(name = "定时任务执行类")
    private String quartzClass;
    @Schema(name = "定时任务任务时间表达式")
    private String quartzCron;

    /**
     * 判断是否有限
     *
     * @param nowTime         当前时间
     * @param orderExpireTime 过期时间
     * @return true 有效 false 无效
     */
    public boolean hasValid(LocalDateTime nowTime, LocalDateTime orderExpireTime) {
        return hasValid(nowTime, orderExpireTime, false);
    }

    /**
     * 判断是否有限
     *
     * @param nowTime         当前时间
     * @param orderExpireTime 过期时间
     * @param defaultState    默认值
     * @return true 有效 false 无效
     */
    public boolean hasValid(LocalDateTime nowTime, LocalDateTime orderExpireTime, boolean defaultState) {
        if (orderExpireTime == null) {
            return defaultState;
        }
        if (nowTime == null) {
            return defaultState;
        }
        if (payTime == null || payTime < 1) {
            return true;
        }
        Duration between = LocalDateTimeUtil.between(nowTime, orderExpireTime);
        System.out.println("between.getSeconds() " + between.getSeconds());
        return between.getSeconds() > payTime;
    }

//    public static void main(String[] args) {
//        StartPayConfig config = new StartPayConfig();
//        config.setPayTime(20);
//        LocalDateTime now = LocalDateTimeUtil.parse("2022-09-15 11:55:10", "yyyy-MM-dd HH:mm:ss");
//        LocalDateTime orderExpireTime = LocalDateTimeUtil.parse("2022-09-15 11:55:21", "yyyy-MM-dd HH:mm:ss");
//        boolean valid = config.hasValid(now, orderExpireTime, false);
//        System.out.println(valid);
//        System.out.println(valid);
//    }
}
